$(function () {
    jsHelper.init();
});
var jsHelper = {
    localData: null,
    templateList: $("#template_list").html(),
    params: {
        keyword: "",
        status: -1,
        pageIndex: 1
    },
    init: function () {
        var self = this;
        hotUtil.loading.show();
        hotUtil.ajaxCall("/goods/ajax/goodslist", self.params, function (ret) {
            hotUtil.loading.close();
            if (ret.resultCode === 2000) {
                var data = ret.data;
                self.localData = data.list;
                var appendHtml = '';
                $.each(data.list, function (i, item) {
                    var tempHtml = self.templateList;
                    tempHtml = tempHtml.replace(/{index}/gm, i + 1);
                    tempHtml = tempHtml.replace(/{goodsId}/gm, item.goodsId);
                    tempHtml = tempHtml.replace(/{goodsName}/gm, item.goodsName);
                    tempHtml = tempHtml.replace(/{goodsPrice}/gm, item.price);
                    tempHtml = tempHtml.replace(/{wholesalePrice}/gm, item.wholesalePrice);
                    tempHtml = tempHtml.replace(/{quotaCount}/gm, item.quotaCount);
                    tempHtml = tempHtml.replace(/{retailStore}/gm, item.retailStore);
                    tempHtml = tempHtml.replace(/{totalWholesaleStore}/gm, item.totalWholesaleStore);
                    tempHtml = tempHtml.replace(/{createTime}/gm, item.createTime);
                    if (item.marketable == 1) {
                        tempHtml = tempHtml.replace(/{status}/gm, 0);
                        tempHtml = tempHtml.replace(/{goodsStatus}/gm, '已上架');
                        tempHtml = tempHtml.replace(/{btnText}/gm, '下架');
                    }
                    else {
                        tempHtml = tempHtml.replace(/{status}/gm, 1);
                        tempHtml = tempHtml.replace(/{goodsStatus}/gm, '已下架');
                        tempHtml = tempHtml.replace(/{btnText}/gm, '上架');
                    }

                    appendHtml += tempHtml;
                });
                $("#table-content").html(appendHtml);

                var pageinate = new hotUtil.paging(".pagination", self.params.pageIndex, 20, data.totalPages, data.totalRecords, 7);
                pageinate.init(function (p) {
                    goTo(p, function (page) {
                        jsHelper.params.pageIndex = page;
                        jsHelper.init();
                    });
                });
            }
        });
    },
    getModel: function (dataId) {
        var model = null;
        if (this.localData != null && this.localData.length > 0) {
            $.each(this.localData, function (i, item) {
                if (item.goodsId === dataId) {
                    model = item;
                    return false;
                }
            });
        }
        return model;
    },
    edit: function (dataId) {
        var data = this.getModel(dataId);
        if (data != null)
            hotUtil.newTab("goods/edit?goodsId=" + dataId, "编辑产品[" + data.goodsName + "]");
        else
            hotUtil.newTab("goods/edit?goodsId=0", "添加产品");
    },
    search: function () {
        this.params.pageIndex = 1;
        this.params.keyword = $("#keyword").val();
        this.params.status = $("#sl_status").val();
        this.init();
    },
    searchAll: function () {
        $("#keyword").val("");
        $("#sl_status").val(-1);
        this.params.pageIndex = 1;
        this.params.keyword = "";
        this.params.status = -1;
        this.init();
    },
    updateStatus: function (goodsId, status) {
        var self = this;
        hotUtil.loading.show();
        hotUtil.ajaxCall("/goods/ajax/updategoodsstatus", {
            goodsId: goodsId,
            status: status
        }, function (ret) {
            hotUtil.loading.close();
            if (ret.resultCode === 2000) {
                self.init();
                swal("设置成功", "", "success");
            }
            else {
                swal("设置失败", "", "error");
            }
        })
    },
    delete: function (dataId) {
        var self = this;
        swal({
            title: "您确定要删除这条信息吗",
            text: "删除后将无法恢复，请谨慎操作！",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "删除",
            closeOnConfirm: false
        }, function () {
            hotUtil.loading.show();
            hotUtil.ajaxCall("/goods/ajax/deletegoods", {
                goodsId: dataId
            }, function (ret) {
                hotUtil.loading.close();
                if (ret.resultCode === 2000) {
                    self.init();
                    swal("删除成功！", "您已经永久删除了这条信息。", "success");
                }
                else {
                    swal("删除失败！", ret.resultMsg, "error");
                }
            });
        });
    },
    setStoreCount: function (dataId) {
        $("#goodsId").val(dataId);
    },
    btnSubmit: function () {
        var self = this;
        var count = $.trim($("#storeCount").val());
        if (hotUtil.isNullOrEmpty(count) || isNaN(parseInt(count))) {
            swal("保存失败！", "库存只能是整数", "error");
            return;
        }
        hotUtil.loading.show();
        hotUtil.ajaxCall("/goods/ajax/changeStoreCount", {
            goodsId: $("#goodsId").val(),
            count: count,
            remark: $("#remark").val()
        }, function (ret) {
            hotUtil.loading.close();
            if (ret.resultCode === 2000) {
                self.init();
                $('#setGoodsStoreCount').modal('hide');
                swal("保存成功！", "", "success");
            }
            else {
                swal("保存失败！", ret.resultMsg, "error");
            }
        });
    }
}